package com.lishangshan.oas_system.controller.attendce;

import com.lishangshan.oas_system.model.dao.attendcedao.AttendceDao;
import com.lishangshan.oas_system.model.dao.attendcedao.AttendceService;
import com.lishangshan.oas_system.model.dao.system.StatusDao;
import com.lishangshan.oas_system.model.dao.system.TypeDao;
import com.lishangshan.oas_system.model.dao.time.TimeUtilsDao;
import com.lishangshan.oas_system.model.dao.user.UserDao;
import com.lishangshan.oas_system.model.dao.user.UserService;
import com.lishangshan.oas_system.model.entity.attendce.Attends;
import com.lishangshan.oas_system.model.entity.system.SystemStatusList;
import com.lishangshan.oas_system.model.entity.user.User;
import com.lishangshan.oas_system.utils.ParameterUtil;
import com.lishangshan.oas_system.utils.StringtoDateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/")
public class AttendceController {
    @Autowired
    private AttendceDao attendceDao;
    @Autowired
    AttendceService attendceService;
    @Autowired
    UserDao uDao;
    @Autowired
    UserService userService;
    @Autowired
    TypeDao typeDao;
    @Autowired
    StatusDao statusDao;
    // 格式转化导入
    DefaultConversionService service = new DefaultConversionService();
    @Autowired
    private TimeUtilsDao timeUtilsDao;

    /**
     * 用户 签到 控制器
     *
     * @param session
     * @param model
     * @param request
     * @return
     * @throws UnknownHostException
     */

    // 考勤 前面的签到
    @RequestMapping("singin")
    public String Datag(HttpSession session, Model model, HttpServletRequest request) throws UnknownHostException {
        //首先获取ip
        InetAddress ia = null;
        ia = ia.getLocalHost();
        String attendip = ia.getHostAddress();
        // 时间规范 获取 规定 上班 时间 和下班 时间
        String start = ParameterUtil.BEON_TIMER, end = ParameterUtil.BEOFF_TIMER;
        System.out.println(start + "  " + end);
        service.addConverter(new StringtoDateUtil());
        // 状态默认是正常
        long typeId, statusId = 10;
        Attends attends = null;
        Long userId = Long.parseLong(session.getAttribute("userId") + "");
        User user = uDao.findOne(userId);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String nowdate = sdf.format(date);
        // 星期 判断该日期是星期几
        SimpleDateFormat sdf3 = new SimpleDateFormat("EEEE");
        // 截取时分
        SimpleDateFormat sdf4 = new SimpleDateFormat("HH:mm");
        // 截取时分秒
        SimpleDateFormat sdf5 = new SimpleDateFormat("HH:mm:ss");

        // 一周当中的星期几
        String weekofday = sdf3.format(date);
        // 时分
        String hourmin = sdf4.format(date);

        // 时分秒
        String hourminsec = sdf5.format(date);
        //System.out.println("星期" + weekofday + "时分" + hourmin + "时分秒" + hourminsec);
        //System.out.println(date);
        Long aid = null;

        // 查找用户当天的所有记录
        Integer count = attendceDao.countrecord(nowdate, userId);
        if (hourminsec.compareTo(ParameterUtil.BEOFFMAX_TIMER) > 0) {
            // 在20点之后签到无效
            System.out.println("----不能签到");
            model.addAttribute("error", "1");
        }
        if (hourminsec.compareTo(ParameterUtil.BEONMIN_TIMER) < 0) {
            //在凌晨5点之前不能签到
            System.out.println("----不能签到");
            model.addAttribute("error", "2");
        } else if ((hourminsec.compareTo(ParameterUtil.BEONMIN_TIMER) > 0) && (hourminsec.compareTo(ParameterUtil.BEOFFMAX_TIMER) < 0)) {
            // 明确一点就是一个用户一天只能产生两条记录
            if (count == 0) {
                if (hourminsec.compareTo(end) < 0) {
                    // 没有找到当天的记录就表示此次点击是上班 就是用来判断该记录的类型
                    // 上班id8
                    typeId = 8;
                    // 上班就只有迟到和正常
                    if (hourminsec.compareTo(start) > 0) {
                        // 迟于规定时间 迟到
                        statusId = 11;
                    } else if (hourminsec.compareTo(start) < 0) {
                        statusId = 10;
                    }
                    attends = new Attends(typeId, statusId, date, hourmin, weekofday, attendip, user);
                    attendceDao.save(attends);
                }
            }
            if (count == 1) {
                // 找到当天的一条记录就表示此次点击是下班
                // 下班id9
                typeId = 9;
                // 下班就只有早退和正常
                if (hourminsec.compareTo(end) > 0) {
                    // 在规定时间晚下班正常
                    statusId = 10;
                } else if (hourminsec.compareTo(end) < 0) {
                    // 在规定时间早下班早退
                    statusId = 12;
                }
                attends = new Attends(typeId, statusId, date, hourmin, weekofday, attendip, user);
                attendceDao.save(attends);
            }
            if (count >= 2) {
                // 已经是下班的状态了 大于2就是修改考勤时间了
                // 下班id9
                if (hourminsec.compareTo(end) > 0) { // 最进一次签到在规定时间晚下班正常
                    statusId = 10;
                } else if (hourminsec.compareTo(end) < 0) {
                    // 最进一次签到在规定时间早下班早退
                    statusId = 12;
                }
                aid = attendceDao.findoffworkid(nowdate, userId);
                Attends attends2 = attendceDao.findOne(aid);
                attends2.setAttendsIp(attendip);
                attendceDao.save(attends2);
                attendceService.updatetime(date, hourmin, statusId, aid);
                Attends aList = attendceDao.findlastest(nowdate, userId);
            }
        }
        // 显示用户当天最新的记录
        Attends aList = attendceDao.findlastest(nowdate, userId);
        if (aList != null) {
            String type = typeDao.findname(aList.getTypeId());
            SystemStatusList statusList = statusDao.findOne(aList.getStatusId());
            model.addAttribute("type", type);
            model.addAttribute("status", statusList);
        }
        model.addAttribute("alist", aList);
        return "systemcontrol/signin";
    }
}
